﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CommonControl;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmDepartmentNew : XtraForm
    {
        public FrmDepartmentNew()
        {
            InitializeComponent();
            InitGrid();
        }

        private void FrmDepartmentNew_Load(object sender, EventArgs e)
        {
            LoadData();

            tlsMain.NodesIterator.DoOperation(new CommonControl.DxGridHelper.CollapseExceptSpecifiedOperation(tlsMain.FocusedNode));
        }

        private void LoadData()
        {
            var bll = new DMZS.BLL.DepartmentNew();
            try
            {
                tlsMain.DataSource = bll.GetListForUIExpand(string.Empty).Tables[0];
            }
            catch (Exception ex)
            {
                Utils.WriteTxtLog(ex);
                XtraMessageBox.Show(Utils.GetRuntimeMessage(ex), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            System.Data.DataTable dt = new System.Data.DataTable();
           tlsMain.ExpandAll();
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            CommonControl.DxGridHelper.InitGridStyle(tlsMain);
        }

        private void tlsMain_CustomDrawNodeIndicator(object sender, DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmDepartmentNew_Editor();
            frm.ID = 0;
            if (tlsMain.FocusedNode != null)
            {
                frm.ParentID = Convert.ToInt32(tlsMain.FocusedNode["ParentID"]);
            }
            else
            {
                frm.ParentID = 0;
            }
            frm.ShowDialog();
        }

        private void btnNewChild_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmDepartmentNew_Editor();
            frm.ID = 0;
            if (tlsMain.FocusedNode != null)
            {
                frm.ParentID = Convert.ToInt32(tlsMain.FocusedNode["ID"]);
            }
            else
            {
                frm.ParentID = 0;
            }
            frm.ShowDialog();
        }

        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (tlsMain.FocusedNode != null)
            {
                var frm = new FrmDepartment_Editor();
                frm.ID = Convert.ToInt32(tlsMain.FocusedNode["ID"]);
                frm.ShowDialog();
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (tlsMain.FocusedNode == null) return;

            var text = string.Empty;
            if (tlsMain.FocusedNode.Nodes.Count != 0)
            {
                text = string.Format("确认删除,其下有{0}子项？\r注意：将同时删除子项！", tlsMain.FocusedNode.Nodes.Count);
            }
            else
            {
                text = "确认删除？";
            }
            if (DialogResult.No == XtraMessageBox.Show(text, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                return;
            }

            var bll = new DMZS.BLL.DepartmentNew();
            int result = bll.DeleteExpand(Convert.ToInt32(tlsMain.FocusedNode["ID"]));
            if (result > 0)
            {
                //XtraMessageBox.Show("删除成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else if (result == -1)
            {
                XtraMessageBox.Show("删除失败，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }
            else if (result == -2)
            {
                XtraMessageBox.Show("该类别或其子类别已被引用，无法删除。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }

            LoadData();
        }

        private void tlsMain_DoubleClick(object sender, EventArgs e)
        {
            if (tlsMain.FocusedNode != null)
            {
                if (tlsMain.FocusedNode.HasChildren) return;

                var frm = new FrmDepartmentNew_Editor();
                frm.ID = Convert.ToInt32(tlsMain.FocusedNode["ID"]);
                frm.ShowDialog();
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }
    }
}
